package com.aaa.cts.service.impl;

import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cts.dao.LoginLogDao;
import com.aaa.cts.entity.LoginLog;
import com.aaa.cts.service.LoginLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统访问记录(LoginLog)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 23:42:08
 */
@Service("loginLogService")
public class LoginLogServiceImpl implements LoginLogService {
    @Resource
    private LoginLogDao loginLogDao;

    /**
     * 根据用户名称查询
     * @param userName
     * @return
     */
    @Override
    public LoginLog selectOneByUserName(String userName) {
        LoginLog loginLog = new LoginLog();
        loginLog.setLoginName(userName);
        loginLog.setStatus("0");
        List<LoginLog> loginLogs=loginLogDao.queryAll(loginLog);
        if(!CollectionUtils.isEmpty(loginLogs)){
            return loginLogs.get(0);
        }
        return null;
    }
    /**
     * 按年月统计操作记录
     * @param yearMonth
     * @return
     */
    @Override
    public Result LoginLogShowByMonth(String yearMonth) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.loginLogDao.LoginLogShowByMonth (yearMonth));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param infoId 主键
     * @return 实例对象
     */
    @Override
    public LoginLog queryById(Integer infoId) {
        return this.loginLogDao.queryById(infoId);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param loginLog 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, LoginLog loginLog) {
        PageHelper.startPage(pageNo,pageSize);
        List<LoginLog> loginLogs = this.loginLogDao.queryAll(loginLog);
        PageInfo<LoginLog> loginLogPageInfo = new PageInfo<>(loginLogs);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), loginLogPageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LoginLog> queryAllByLimit(int offset, int limit) {
        return this.loginLogDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param loginLog 实例对象
     * @return 实例对象
     */
    @Override
    public LoginLog insert(LoginLog loginLog) {
        this.loginLogDao.insert(loginLog);
        return loginLog;
    }

    /**
     * 修改数据
     *
     * @param loginLog 实例对象
     * @return 实例对象
     */
    @Override
    public LoginLog update(LoginLog loginLog) {
        this.loginLogDao.update(loginLog);
        return this.queryById(loginLog.getInfoId());
    }

    /**
     * 通过主键删除数据
     *
     * @param infoId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer infoId) {
        return this.loginLogDao.deleteById(infoId) > 0;
    }
}